import {getRequest} from "./api";
import router from "../router";
import store from "../store";

export  const initMenu=(router,store)=>{
    if (store.state.routes.length>0){
        return;
    }
}

getRequest('/house/customer/getCustomerInfo').then(res => {
    getRequest('/listResource/' + res.data.user.roleId)
        .then(data => {
            //console.log(data)
            if (data) {
                //  console.log(data);
                //格式化好的
                let fmRoutes = formatRoutes(data);
                //console.log(JSON.stringify(fmRoutes)+"111111111");
                //添加到；路由中
                initMenu(router, store);
                for (let i = 0, length = fmRoutes.length; i < length; i += 1) {
                    const element = fmRoutes[i];
                    router.addRoute(element);
                }
                //把他们放到vuex里面
                store.commit('initRoutes', fmRoutes)
            }
        })
})


getRequest('/getUserInfo')
    .then(res => {
        getRequest('/listResource/' + res.data.user.roleId)
            .then(data => {
                //console.log(data)
                if (data) {
                    //  console.log(data);
                    //格式化好的
                    let fmRoutes = formatRoutes(data);
                    //console.log(JSON.stringify(fmRoutes)+"111111111");
                    //添加到；路由中
                    initMenu(router, store);
                    for (let i = 0, length = fmRoutes.length; i < length; i += 1) {
                        const element = fmRoutes[i];
                        router.addRoute(element);
                    }
                    //把他们放到vuex里面
                    store.commit('initRoutes', fmRoutes)
                }

            })
    })

        export const formatRoutes = (routes) => {
    let fmRoutes = [];
    routes.forEach(router => {
            let {
                path,
                component,
                name,
                icon,
                children,
            } = router;
            //console.log(component + "asdsadsadsaasd")
            if (children && children instanceof Array) {
                //递归
                children = formatRoutes(children);
            }

            let fmRouter = {
                path: path,
                name: name,
                icon: icon,
                children: children,
                component(resolve) {
                    if (component.startsWith('Home')) {
                        require(['../views/' + "Home" + '.vue'], resolve)
                    } else if (component.startsWith('Page')) {
                        require(['../views/page/' + component + '.vue'], resolve);
                    } else if (component.startsWith('Vitalize')) {
                        require(['../views/vitalize/' + component + '.vue'], resolve);
                    } else if (component.startsWith('Buap')) {
                        require(['../views/buap/' + component + '.vue'], resolve);
                    } else if (component.startsWith('Sys')) {
                        require(['../views/sys/' + component + '.vue'], resolve);
                    }
                }

            }

            fmRoutes.push(fmRouter);

    })
    return fmRoutes;
}


// export const formatRoutes = (routes)=>{
//     //定义一个空数组
//     let fmRoutes=[];
//     routes.forEach=(router=>{
//         let {
//             path,
//             component,
//             name,
//             icon,
//             children,
//         }= router;
//         //alert(router);
//         //进行判断你的二级菜单是否存在，是否是数组
//         if (children && children instanceof Array){
//             //递归调用
//             children = formatRoutes(children);
//         }
//         //单独对某一个路由进行格式化
//         let fmRouter = {
//                 path:path,
//                 name:name,
//                 icon:icon,
//                 children:children,
//                 //这样就可以对应路由的组件了
//                 component(resolve){
//                     if(component.startsWith('Vit')){
//                         require(['../views/vit/+component'.vue],resolve)
//                     }else if(component.startsWith('Bua')){
//                         require(['../views/bua/+component'.vue],resolve)
//                     }else if(component.startsWith('Sys')){
//                         require(['../views/sys/+component'.vue],resolve)
//                     }
//
//                 }
//
//         }
//         //alert(fmRouter);
//         console.log(fmRouter)
//         //把格式化好的东西放到数组里
//         fmRoutes.push(fmRouter)
//     });
//     return fmRoutes;
// }